<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of project
 *
 * @author nstevens
 */
require_once 'controller/commonController.php';
require_once 'model/projectModelClass.php';
require_once 'view/projectViewClass.php';
require_once 'utility.php';

class projectControllerClass extends commonController {

    public function __construct($model) {
        $this->model = $model;
    }

    //actions*******************************************************************
    //****VIEWS*****************************************************************

    /*
     * Displays a table of projects not proposals
     */
    function viewProjectDetailsRequest() {
        $this->model->init($_GET['projectId'], 2);
        if (isset($_GET['activeDiv'])) {
            $activeDiv = $_GET['activeDiv'];
        } else {
            $activeDiv = NULL;
        }
        $this->model->string = $this->model->getProjectDetailsPage($this->model, $activeDiv);
    }

    /*
     * Displays the projects that are related to the currently logged in PI
     */

    function viewMyProjectsRequest() {
        $curUser = $_SESSION['username'];
        $personID = getPersonIDFromUsername($curUser);
        $projectObjArray = $this->model->getProjectsThatThisPersonIsRelatedTo($personID);
        $this->model->string = "";
        $this->model->string .= $this->model->getBasicHeader("View Projects");
        $this->model->string .= $this->model->getProjectSummaryTable($projectObjArray);
        $this->model->string .= '<br><br> ' . getWebmasterFeedbackForm();
    }

    /*
     * Displays the page that allows the user to enter rebaseline information into the financials table
     */

    function viewProjectRebaselinePage() {
        //echo 'hi';
        $this->model->init($_GET['projectId'], 2);
        $this->model->string .= $this->model->getProjectRebaselinePage();
    }

    /*
     * Displays projects in a table (not proposals)
     */

    function viewProjectsRequest() {
        //$projectObjArray = $this->model->getAllProjects(1);
        $projectObjArray = $this->model->getProjectObjects(0, NULL, 2);
        $this->model->string = "";
        $this->model->string .= $this->model->getBasicHeader("View Projects");
        $this->model->string .= $this->model->getProjectSummaryTable($projectObjArray);
    }

    /*
     * Displays proposals in a table (not projects)
     */

    function viewProposalsRequest() {
        $projectObjArray = $this->model->getProjectObjects(0,NULL,1);
        $this->model->string = "";
        $this->model->string .= $this->model->getBasicHeader("View Proposals");
        $this->model->string .= $this->model->getProjectSummaryTable($projectObjArray);
    }

    /*
     * Generates the page to view the details of a contract
     */

    function viewContractDetailsRequest() {
        $this->model->init($_GET['projectId'], 2);
        $this->model->string = $this->model->getContractDetailsPage();
    }

    //****FORMS*****************************************************************

    /*
     * Generates the form to create a new project
     */
    function proposeProjectRequest() {
        if (isset($_POST['fyDropdown'])) {
            $fy = $_POST['fyDropdown'];
            $this->model->string = $this->model->getproposeProjectRequestPage($fy);
        } else {
            $this->model->string = $this->model->getproposeProjectRequestSelectYearPage();
        }
    }

    /*
     * Generates the form to edit an existing project
     */
    function editExistingProposalRequest() {
        $this->model->init($_POST['projectID'], 1);
        $this->model->string = $this->model->getEditProjectPage();
    }
    
    /*
     * Generates the form to create a new task
     */

    function proposeNewTaskRequest() {
        $this->model->init($_POST['projectID'], 2);
        $this->model->string = $this->model->getNewTaskPage();
    }

    /*
     * Generates the form to create a contract
     */
    function proposeNewContractRequest() {
        $this->model->init($_POST['projectID'], 1);
        $this->model->string = $this->model->getNewContractPage();
    }
    
    /*
     * Generates the form to edit an existing contract
     */
    function editExistingContractRequest() {
        $this->model->init($_POST['projectID'], 1);
        $this->model->string = $this->model->getEditContractPage();
    }

    //****PROCESSING************************************************************
    /*
     * Processing of existing project buttons
     */
    function processExistingProjectRequest() {
        //echo $_POST['submit'];
        //return;
        if (isset($_POST['submit'])) {
            if (strcmp("Save Expense", $_POST['submit']) === 0) {
                $this->processNonLaborSubmissionRequest();
            } else if (strcmp("Edit Contract", $_POST['submit']) === 0) {
                $this->editExistingContractRequest();
            } else if (strcmp("Delete Contract", $_POST['submit']) === 0) {
                $this->processDeleteContractRequest();
            } else if (strcmp("Save Contract", $_POST['submit']) === 0) {
                $this->processExistingContractRequest();
            } else if (strcmp("Activate", $_POST['submit']) === 0) {
                $this->processActivateTask();
            } else if (strcmp("Edit Project", $_POST['submit']) === 0) {
                $this->editExistingProposalRequest();
            } else if (strcmp("Add Notes", $_POST['submit']) === 0) {
                $this->processAddNotesRequest();
            } else if (strcmp("Upload Report", $_POST['submit']) === 0) {
                $this->processUploadReportRequest();
            } else {
                $this->processChangeTaskStatusRequest();
            }
        }
    }

    /*
     * Called when a PI reviewer has approved/disapproved a rebaseline submission (actual new proposed values)
     */
    function processRebaselineSubmitDecisionRequest() {
        $url = $this->model->processRebaselineSubmitDecision();
        if ($url) {
            header($url);
        } else {
            echo "error uploading";
        }
    }

    /*
     * Called when a PI reviewer has approved/disapproved a rebaseline request (no values)
     */
    function processRebaselineSubmitRequest() {
        $url = $this->model->processRebaselineSubmit();
        if ($url) {
            header($url);
        } else {
            echo "error uploading";
        }
    }

    /*
     * Called when a PI has selected to upload report button
     */
    function processUploadReportRequest() {
        //print_r($_POST);
        //return;
        $url = $this->model->processUploadReport();
        if ($url) {
            header($url);
        } else {
            echo "error uploading";
        }
    }

    /*
     * Called when there is a project status change (usually buttons in the project status div)
     */
    function processChangeStatusRequest() {
        //request rebaseline button
        $url = $this->model->processChangeStatus();
        if ($url) {
            header($url);
        } else {
            echo "error changing status";
        }
    }

    /*
     * Called when a PI has clicked continue on the new Proposal form
     */
    function processNewProposalSubmitRequest() {
        $url = $this->model->processNewProposalSubmit();
        if ($url) {
            header($url);
        } else {
            echo "error processing new proposal";
        }
    }

    /*
     * Called when a PI is submitting a new contract
     */
    function processNewContractRequest() {
        $this->model->init($_POST['projectID'], 1);
        $url = $this->model->processNewContract();
        if ($url) {
            echo header($url);
        } else {
            echo "Error processing new labor";
        }
    }

    /*
     * Called when a PI is submitting a change to an existing contract
     */
    function processExistingContractRequest() {
        $this->model->init($_POST['projectID'], 1);
        $url = $this->model->processExistingContract();
        if ($url) {
            echo header($url);
        } else {
            echo "Error processing existing labor";
        }
    }

    /*
     * Called when a PI is submitting non labor expenses
     */
    function processNonLaborSubmissionRequest() {
        $this->model->init($_GET['projectId'], 1);
        $url = $this->model->processExistingNonLabor();
        if ($url) {
            echo header($url);
        } else {
            echo "Error processing existing labor";
        }
    }
    
    /*
     * Called when a PI deletes a contract
     */
    function processDeleteContractRequest() {
        $this->model->init($_POST['projectID'], 1);
        $url = $this->model->processDeleteContract();
        if ($url) {
            echo header($url);
        } else {
            echo "Error deleting existing contract";
        }
    }

    /*
     * Processes the decision of the reviewer to allow the PI to submit a rebaseline 
     */
    public function processRebaselineDecisionRequest() {
        $url = $this->model->processRebaselineDecision();
        if ($url) {
            header($url);
        } else {
            echo "error";
        }
    }

    //OTHER STUFF***************************************************************
}

?>
